(function($, doc) {
	$.init();
	var accountBox = doc.getElementById('account');
	var getCodeBox = doc.getElementById('getCode');
	var loginButton = doc.getElementById('login');
	var codeBox = doc.getElementById('code');
	/*
	var lastUser = app.getOneUser();
	if(lastUser && lastUser.user_id) {
		accountBox.value = lastUser.phone;
	}*/ 
	function canLogin() {
		if(accountBox.value != '' && codeBox.value != '') {
			loginButton.removeAttribute('disabled');
		} else {
			loginButton.setAttribute('disabled', 'disabled');
		}
	}
	accountBox.addEventListener('input', canLogin);
	codeBox.addEventListener('input', canLogin);
	mui('.mui-input-row').on('tap', '.mui-icon.mui-icon-clear', function() {
		loginButton.setAttribute('disabled', 'disabled');
	});
	getCodeBox.addEventListener('tap', function() {
		//localStorage.removeItem('_send_code');
		var c1 = 60,
			phone = accountBox.value;
		if(!checkPhone(phone)) {
			return;
		}
		var btnArray = ['确定', '取消'];
		mui.confirm('我们将发送验证码短信到下面的号码：\n' + phone, '确认手机号码', btnArray, function(e) {
			if(e.index == 0) {
				mui.get(apiurl + 'index/sms', {
					'phone': phone
				}, function(res) {
					console.log(JSON.stringify(res));
					if(res.status == 1) {
						plus.nativeUI.toast('发送短信成功');
						//发送短信成功
						//localStorage.setItem('_send_code', res.ck_code);
					} else {
						plus.nativeUI.toast(res.msg);
						getCodeBox.innerHTML = '获取验证码';
						getCodeBox.removeAttribute('disabled');
						c1 = 60;
						return;
					}
					var code1 = setInterval(function() {
						getCodeBox.innerHTML = c1 + '秒后重新获取';
						c1 = c1 - 1;
						if(c1 >= 0) {
							getCodeBox.setAttribute("disabled", "disabled");
						} else {
							getCodeBox.innerHTML = '获取验证码';
							getCodeBox.removeAttribute('disabled');
							c1 = 60;
							clearInterval(code1);
						}
					}, 1000);
				});
			}

		})

	});

	loginButton.addEventListener('tap', function(event) {
		var loginInfo = {
			account: accountBox.value,
			code: codeBox.value,
			login_type: 2
		};
		if(!checkPhone(loginInfo.account)) {
			return;
		}
		if(loginInfo.code == '' || loginInfo.code.length < 4) {
			plus.nativeUI.toast('请填写验证码');
			return;
		}
		ajaxlogin(loginInfo, '../../main.html');
	});
	doc.getElementById('reg').addEventListener('tap', function() {
		//location.href = '../../reg.html';
		mui.openWindow('../../reg.html', 'reg');
	});
	doc.getElementById('sms_pwd').addEventListener('tap', function() {
		location.href = 'loginbypwd.html';
	}, false);
}(mui, document));